package com.ly.work.zk;

import com.alibaba.fastjson.JSON;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;

public class Many619 {
    //声明数据大小
    public final static Integer DataSum = 10000;
    //声明组织大小
    public final static Integer DataGroup = 10;
    //声明线程控制数量
    public final static Integer DataThread = 5;

    public  static List<Integer> datalist=new ArrayList<Integer>();

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        //添加数据
        for (int i = 0; i < DataSum; i++) {
            datalist.add(i);
        }

        //处理数据
        dataWork(datalist);

    }

    private static void dataWork(List<Integer> datalist) throws ExecutionException, InterruptedException {
        List<List<Integer>> grouplist = new ArrayList<List<Integer>>();
        //每组10个元素
        int arraysize = datalist.size() / DataGroup;
        for (int i = 0; i < arraysize; i++) {
            grouplist.add(datalist.subList(i * DataGroup, (i + 1) * DataGroup));
        }

        //System.out.println(grouplist);

        int threadsize = grouplist.size()/DataThread;
        //创建线程池
        ExecutorService executorService = Executors.newFixedThreadPool(threadsize);
        //第二次分割数据
        Object result = 0;
        for (int l = 0; l < threadsize; l++) {
            List<List<Integer>> sublist = grouplist.subList(l * DataThread, (l + 1) * DataThread);

            CallableTest callableTest = new CallableTest(sublist);

            Future submit = executorService.submit(callableTest);

            result  =  submit.get();
        }
        System.out.println("共处理"+result+"条数据");
    }

}
class CallableTest implements Callable {

    static AtomicInteger sum = new AtomicInteger();

    private List<List<Integer>> sublist =null;

    public CallableTest(List<List<Integer>> sublist) {
        this.sublist = sublist;
    }

    @Override
    public Object call() throws Exception {
        //成功记数
        AtomicInteger success = new AtomicInteger();
        //失败记数
        AtomicInteger fire = new AtomicInteger();
        try {
            synchronized (Object.class) {
                for (List<Integer> integerList : sublist) {
                    for (Integer integer : integerList) {
                        if (integer.intValue()%2==0){
                            //成功处理
                            sum.incrementAndGet();
                            success.incrementAndGet();
                        }else {
                            fire.incrementAndGet();
                            if ((integer.intValue()+1)%2==0){
                                sum.incrementAndGet();
                            }
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            System.out.println("成功"+success+"失败"+fire+ JSON.toJSONString(sublist));
        }
        return sum.get();
    }
}

